package com.atstudy.headline.web.app.controller.tag;

import com.atstudy.headline.entity.Contents;
import com.atstudy.headline.entity.Tags;
import com.atstudy.headline.result.Result;
import com.atstudy.headline.web.app.service.TagsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/app/tags")
public class AppTagController {

    @Autowired
    private TagsService tagsService;

    /**
     * 标签列表
     * @return
     */
    @GetMapping("/hot")
    public Result<List<Tags>> List() {
        LambdaQueryWrapper<Tags> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Tags::getIsHot, 1);
        // 按照使用次数ID降序排序
        queryWrapper.orderByDesc(Tags::getUsageCount);
        // 只查询前5条数据
        queryWrapper.last("limit 7");
        List<Tags> tags = tagsService.list(queryWrapper);
        return Result.ok(tags);
    }

    /**
     * 根据标签id查询内容详情
     * @param tagId
     * @return
     */

    @GetMapping("/{tagId}")
    public Result<List<Contents>> detail(@PathVariable Integer tagId) {
        List<Contents> contents = tagsService.selectTagIdByContent(tagId);
        return Result.ok(contents);
    }


    /**
     * 查询全部标签
     * @return
     */

    @GetMapping("/all")
    public Result<List<Tags>> all() {
        return Result.ok(tagsService.list());
    }


}
